<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>计划与实施</title>
    <meta charset="UTF-8">
    <title>新闻分页展示</title>
    <!--vue-->
    <script src="/js/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/css/index.css">
    <!-- 引入组件库 -->
    <script src="/js/index.js"></script>
    <!--axios-->
    <script src="/js/axios.min.js"></script>
</head>
<style>
    .el-table .success-row {
        background: #f0f9eb;
    }
</style>
<body>
<div id="app">
    <el-row>
        <el-col :span="4"><el-input v-model="coursename"   size="mini" placeholder="课程名称"></el-input></el-col>
        <el-col :span="4"><el-input v-model="userid"   size="mini" placeholder="培训人工号"></el-input></el-col>
        <el-col :span="4"><el-button type="primary" size="mini" @click="search()" icon="el-icon-search">搜索</el-button></el-col>
        <el-col :span="8">&nbsp;</el-col>
        <el-col :span="2"><el-button type="primary" size="small" round
            @click="toEdit()"class="el-icon-edit">添加</el-button></el-col>
        <el-col :span="2"><el-button type="primary" size="mini" @click="flush()" icon="el-icon-search">刷新</el-button></el-col>
    </el-row>
    <el-row>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName">
            <el-table-column
                    prop="trainid"
                    label="培训计划ID"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="userid"
                    label="培训人工号"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="username"
                    label="培训人姓名">
            </el-table-column>
            <el-table-column
                    prop="trainername"
                    label="培训师">
            </el-table-column>
            <el-table-column
                    prop="coursename"
                    label="培训课程">
            </el-table-column>
            <el-table-column
                    prop="starttime"
                    label="培训开始时间">
            </el-table-column>
            <el-table-column
                    prop="endtime"
                    label="培训结束时间">
            </el-table-column>
            <el-table-column prop="gradetype" label="培训状态">
                <template slot-scope="scope">
                    <span v-if = "scope.row.gradetype==0">培训中</span>
                    <span v-if = "scope.row.gradetype==1">通过</span>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="small" round
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)"class="el-icon-delete"> 删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[2,3,5,10,100, 200, 300, 400]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </el-row>



    <el-row>
        <!--添加弹出框-->
        <el-dialog title="添加" :visible.sync="editFormVisible" >
            <el-form :model="form" ref="form"  :rules="rules" >
                <el-form-item v-if="show" label="培训师id" :label-width="formLabelWidth">
                    <el-input v-model="form.trainerid" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程名称" :label-width="formLabelWidth" prop="courseid">
                    <el-select v-model="form.courseid" placeholder="-请选择-" @change="getTrainerName(form.courseid)" >
                        <el-option
                                v-for="item in options1"
                                :key="item.courseid"
                                :label="item.coursename"
                                :value="item.courseid">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="培训师" :label-width="formLabelWidth" prop="trainerid">
                    <el-select v-model="form.trainerid" placeholder="-请选择-">
                        <el-option
                                v-for="item in options2"
                                :key="item.trainerid"
                                :label="item.trainername"
                                :value="item.trainerid">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="开始时间" :label-width="formLabelWidth" prop="starttime">
                    <el-date-picker
                            v-model="form.starttime"
                            type="date"
                            value_format="yyyy-MM-dd"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" :label-width="formLabelWidth" prop="endtime">
                    <el-date-picker
                            v-model="form.endtime"
                            type="date"
                            value_format="yyyy-MM-dd"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="培训人" :label-width="formLabelWidth" prop="userid">
                    <el-select v-model="form.userid" placeholder="-请选择-">
                        <el-option
                                v-for="item in options3"
                                :key="item.userid"
                                :label="item.username"
                                :value="item.userid">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="add()">添 加</el-button>
            </div>
        </el-dialog>
    </el-row>
</div>
<script>
    var vue = new Vue({
        el:"#app",
        data:{//数据
            tableData:[],
            currentPage:1,
            pageSize:5,
            total:20,
            editFormVisible:false,//编辑对话框是否显示
            form: {
                courseid:'',
                trainerid:'',
                starttime:'',
                endtime:'',
                userid:''
            },//添加form
            rules: {
                courseid: [
                    { required: true, message: '请选择课程名称', trigger: 'change' }
                ],
                trainerid: [
                    { required: true, message: '请选择培训师', trigger: 'change' }
                ],
                starttime: [
                    { type: 'date', required: true, message: '请选择开始日期', trigger: 'change' }
                ],
                endtime: [
                    { type: 'date', required: true, message: '请选择结束日期', trigger: 'change' }
                ],
                userid: [
                    { required: true, message: '请选择培训人', trigger: 'change' }
                ],

            },
            formLabelWidth:"120px",
            userid:null,
            coursename:null,
            options1:[],
            options2:[],
            options3:[],
            courseid:[],
            gradetype:[],
            starttime:'',
            endtime:'',
        },

        created:function () {
            this.loadData();
        },
        methods:{//方法
            //加载数据的方法
            loadData:function () {
                _this =this;
                //初始化数据
                axios.get('/train/page', {
                    params: {
                        pageNo:this.currentPage,
                        pageSize:this.pageSize,
                        userid:this.userid,
                        coursename:this.coursename
                    }
                })
                    .then(function (response) {
                        //赋值分页列表
                        _this.tableData = response.data.page;
                        //删除最后一页为空时，调到上一页
                        if(_this.tableData.length==0&&_this.currentPage!=1){
                            _this.currentPage=_this.currentPage-1;
                            _this.loadData();
                        }
                        //赋值总条数
                        _this.total = response.data.total;
                    })
                    .catch(function (error) {
                        // handle error 处理异常
                        //console.log(error);
                    })
                    .then(function () {
                        // always executed  总是执行  相当于finally
                    });
            },
            tableRowClassName({row, rowIndex}) {
                if (row.gradetype===1){
                    return 'success-row';
                }
                return '';
            },
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            getTrainerName:function(val1){
                //下拉options2
                axios.get('/train/getTrainerName', {
                        params: {
                            courseid: val1
                        }
                    })
                    .then(function (response) {
                        _this.options2 = response.data
                    })
            },

            flush(){
                //刷新当前页
                location.reload();
            },
            //每页数量发生变化时
            handleSizeChange:function (v) {
                this.pageSize=v;
                this.loadData();
            },
            //当前页数放生变化是
            handleCurrentChange:function (v) {
                this.currentPage=v;
                this.loadData();
            },
            // 搜索方法
            search:function () {
                this.currentPage=1;
                this.loadData();
            },
            //跳转编辑
            toEdit:function () {
                console.log("我进来了");
                this.form={};
                //根据id请求
                axios.post('/train/getCourseName ').then(function (response) {
                    _this.options1 = response.data.list;
                })
                axios.post('/train/getUserName ').then(function (response) {
                    _this.options3 = response.data.list;
                })
                this.editFormVisible=true;
            },


            //删除
            handleDelete:function (index,row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post('/train/delete?trainid='+row.trainid).then(function (response) {
                        console.log(response);
                        if(response.status == 200){
                            if(response.data>0) {
                                //提示成功
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                //重新加载数据
                                _this.loadData();
                            }
                        }
                    }).catch(function (error) {
                        console.log(error);
                        _this.$message.error("删除失败");
                    });

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //添加
            add:function(){
                _this = this;
                axios.post('/train/add',this.form).then(function (response) {
                    console.log(response);
                    if(response.status == 200){
                        //提示成功
                        _this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                        //关闭弹出框
                        _this.editFormVisible=false;
                        //重新加载数据
                        _this.loadData();
                        //清空form
                        _this.form={};
                    }
                }).catch(function (error) {
                    console.log(error);
                    _this.$message.error("信息未填充完整");
                });
            },
        }
    });
</script>
</body>
</html>